package com.zq.thread.实现;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: zhaoqi
 * @Date: 11/16/2019 10:23 PM
 * @Version 1.0
 */

public class DemoCallable implements Callable {

    private int i = 0;

    @Override
    public Object call() throws Exception {

        int sum = 0;
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "--" + i);
            sum +=i;
        }
        return sum;
    }

    public static void main(String[] args) {
        Callable<Integer> demoCallable = new DemoCallable();
        FutureTask<Integer> ft = new FutureTask<>(demoCallable);
        for (int i = 0; i < 1000; i++) {
            System.out.println(Thread.currentThread().getName() + "++" +i);
            if ( i == 30){
                Thread thread = new Thread(ft);
                thread.start();
            }
        }
        System.out.println("主程序执行完毕---------------");

        try {
            int sum  = ft.get();
            System.out.println("sum = " + sum );
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


    }

}
